from django.core.paginator import Paginator, EmptyPage
from django.shortcuts import render

# Create your views here.
from django.views import View
from django.http import JsonResponse
from haystack.views import SearchView

from goods.models import GoodsCategory, SKU
from goods.utils import get_breadcrumb

"""商品列表页"""
class ListView(View):
    """商品列表页"""

    def get(self, request, category_id):
        page = request.GET.get('page')  # 当前页码
        page_size = request.GET.get('page_size')    # 每页数量
        ordering = request.GET.get('ordering')  # 排序方式
        # 判断category_id是否正确
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except Exception as e:
            return JsonResponse({'code': 400,
                                 'errmsg': '获取mysql数据出错'})
        breadcrumb = get_breadcrumb(category)
        # 排序方式:
        try:
            skus = SKU.objects.filter(
                category=category,
                is_launched=True).order_by(ordering)
        except Exception as e:
            return JsonResponse({'code': 400,
                                 'errmsg': '获取mysql数据出错'})
        # 分页
        paginator = Paginator(skus, page_size)
        # 获取每页商品数据
        try:
            page_skus = paginator.page(page)
        except EmptyPage:
            # 如果page_num不正确，默认给用户400
            return JsonResponse({'code': 400,
                                 'errmsg': 'page数据出错'})
        # 获取列表页总页数
        total_page = paginator.num_pages
        # list = [{'id':sku.id,
        #          'default_image_url':sku.default_image_url,
        #          'name':sku.name,
        #          'price':sku.price}
        #         for sku in page_skus]
        list = []
        for sku in page_skus:
            list.append({
                'id': sku.id,
                # 'id': 1,
                'default_image_url': sku.default_image_url,
                'name': sku.name,
                'price': sku.price
            })

        return JsonResponse({
            "code": 0,
            "errmsg": "ok",
            "breadcrumb": breadcrumb,  # 面包屑数据
            "count": total_page,  # 分页总数
            "list":list,
        })

"""商品热销排行"""
class HotGoodsView(View):
    """商品热销排行"""
    def get(self, request, category_id):
        # 根据销量倒序
        try:
            skus = SKU.objects.filter(category_id=category_id,
                                      is_launched=True).order_by('-sales')[:2]
        except Exception as e:
            return JsonResponse({'code':400,
                                 'errmsg':'获取商品出错'})
        # 转换格式:
        hot_skus = [{'id':sku.id,
                'default_image_url':sku.default_image_url,
                'name':sku.name,
                'price':sku.price} for sku in skus]

        return JsonResponse({'code': 0,
                             'errmsg': 'OK',
                             'hot_skus': hot_skus})

"""全文检索"""
class MySearchView(SearchView):
    '''重写SearchView类'''
    def create_response(self):
        page = self.request.GET.get('page')
        # 获取搜索结果
        context = self.get_context()
        data_list = []
        for sku in context['page'].object_list:
            data_list.append({
                'id':sku.object.id,
                'name':sku.object.name,
                'price':sku.object.price,
                'default_image_url':sku.object.default_image_url,
                'searchkey':context.get('query'),
                'page_size':context['page'].paginator.num_pages,
                'count':context['page'].paginator.count
            })
        # 拼接参数, 返回
        return JsonResponse(data_list, safe=False)